package chixing.day15.work;

import java.util.HashMap;

public class combine {

    public static HashMap<String, Integer> combineMaps(HashMap<String, Integer> map1, HashMap<String, Integer> map2, HashMap<String, Integer> map3) {
        //如果多个映射中有相同的键，取键对应的最大值
        HashMap<String, Integer> combinedMap = new HashMap<>();
        for (String key : map1.keySet()) {
            combinedMap.put(key, Math.max(map1.get(key), map2.getOrDefault(key, 0)));
            combinedMap.put(key, Math.max(combinedMap.get(key), map3.getOrDefault(key, 0)));
        }
        for (String key : map2.keySet()) {
            combinedMap.put(key, Math.max(combinedMap.getOrDefault(key, 0), map2.get(key)));
            combinedMap.put(key, Math.max(combinedMap.get(key), map3.getOrDefault(key, 0)));
        }
        for (String key : map3.keySet()) {
            combinedMap.put(key, Math.max(combinedMap.getOrDefault(key, 0), map3.get(key)));
            combinedMap.put(key, Math.max(combinedMap.get(key), map3.get(key)));
        }
        return combinedMap;
    }

    public static void main(String[] args) {
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("a", 1);
        map1.put("b", 2);

        HashMap<String, Integer> map2 = new HashMap<>();
        map2.put("b", 3);
        map2.put("c", 4);

        HashMap<String, Integer> map3 = new HashMap<>();
        map3.put("a", 5);
        map3.put("c", 1);

        HashMap<String, Integer> combinedMap = combineMaps(map1, map2, map3);
        System.out.println(combinedMap);
    }
}
